package handler

import (
	"ecomm-manage/cache"
	"ecomm-manage/model"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func Good(c *gin.Context) {
	good_id, _ := strconv.Atoi(c.PostForm("good_id"))
	good_tel := c.PostForm("good_tel")
	good_type := c.PostForm("good_type")
	good_price := c.PostForm("good_price")
	data := model.Good{
		GoodId:    good_id,
		GoodTel:   good_tel,
		GoodType:  good_type,
		GoodPrice: good_price,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
	})
}

func Order(c *gin.Context) {
	order_id, _ := strconv.Atoi(c.PostForm("order_id"))
	order_good := c.PostForm("order_good")
	order_price := c.PostForm("order_price")
	data := model.Order{
		OrderId:    order_id,
		OrderGood:  order_good,
		OrderPrice: order_price,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
	})
}

func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size

	var order []model.GoodsOrder

	redis_key := "user_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("orders").Select(
			"goods.good_id as GoodId",
			"goods.good_tel as GoodTel",
			"goods.good_type as GoodType",
			"goods.good_price as GoodPrice",
			"orders.order_id as OrderId",
			"orders.order_good as OrderGood",
			"orders.order_price as OrderPrice",
		).Joins("LEFT JOIN goods ON goods.id=orders.gid").
			Offset(offset).Limit(size).
			Debug().Find(&order)
		jsonData, _ := json.Marshal(order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &order)
	}
	var total int64
	model.DB.Model(&model.Order{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  order,
		"total": total,
	})
	/*
		SELECT
		goods.good_id as GoodId,
		goods.good_tel as GoodTel,
		goods.good_type as GoodType,
		goods.good_price as GoodPrice,
		orders.order_id as OrderId,
		orders.order_good as OrderGood,
		orders.order_price as OrderPrice,
		FROM
		orders
		LEFT JOIN goods ON goods.id=orders.gid
	*/
}
func OrderDetail(c *gin.Context) {

	id := c.Query("id")
	var order model.GoodsOrder

	redis_key := "order_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("orders").Select(
			"goods.good_id as GoodId",
			"goods.good_tel as GoodTel",
			"goods.good_type as GoodType",
			"goods.good_price as GoodPrice",
			"orders.order_id as OrderId",
			"orders.order_good as OrderGood",
			"orders.order_price as OrderPrice",
		).Joins("LEFT JOIN goods ON goods.id=orders.gid").
			Where("goods.id=?", id).
			Debug().Find(&order)
		jsonData, _ := json.Marshal(order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &order)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": order,
	})
	/*
		SELECT
		goods.good_id as GoodId,
		goods.good_tel as GoodTel,
		goods.good_type as GoodType,
		goods.good_price as GoodPrice,
		orders.order_id as OrderId,
		orders.order_good as OrderGood,
		orders.order_price as OrderPrice,
		FROM
		orders
		LEFT JOIN goods ON goods.id=orders.gid
	*/
}
